import request from "@/utils/request";
import { IObject } from "../common/common";

const BASE_URL = "/system/activity/nodeTask";

const ActivityNodeTaskAPI = {
  /** 获取个人任务 */
  loadCurrentTask(params?: ActivityNodeTaskPageQuery) {
    return request<any, PageResult<ActivityNodeTaskVO[]>>({
      url: `${BASE_URL}/loadCurrentTask`,
      method: "get",
      params,
    });
  },

  /**
   * 根据id 获取流程节点任务详情
   * @param id 任务id
   */
  loadDetail(id: string) {
    return request<any, ActivityNodeTaskDetail>({
      url: `${BASE_URL}/load/${id}`,
      method: "get",
    });
  },

  /**
   * 开始流程节点任务
   * @param id 任务id
   */
  start(id: string) {
    return request<any, boolean>({
      url: `${BASE_URL}/start`,
      method: "get",
      params: { taskId: id },
    });
  },

  /**
   * 完成流程节点任务
   * @param id 任务id
   */
  complete(id: string) {
    return request<any, boolean>({
      url: `${BASE_URL}/complete`,
      method: "get",
      params: { taskId: id },
    });
  },

  /**
   * 填报流程节点任务
   * @param data 表单数据
   */
  fill(data: ActivityNodeTaskFillForm) {
    return request<any, ActivityNodeTaskVO>({
      url: `${BASE_URL}/fill`,
      method: "post",
      data,
    });
  },

  /**
   * 添加流程节点任务
   * @param data 表单数据
   */
  create(data: ActivityNodeTaskForm) {
    return request<any, ActivityNodeTaskVO>({
      url: `${BASE_URL}/add`,
      method: "post",
      data,
    });
  },

  /**
   * 修改流程节点任务
   * @param data 表单数据
   */
  update(data: ActivityNodeTaskForm) {
    return request<any, ActivityNodeTaskVO>({
      url: `${BASE_URL}/update`,
      method: "post",
      data,
    });
  },

  /**
   * 删除任务
   * @param id 任务id
   */
  deleteById(id: string) {
    return request({
      url: `${BASE_URL}/delete/${id}`,
      method: "get",
    });
  },

  /**
   * 审批任务
   * @param data 表单数据
   */
  audit(data: AuditForm) {
    return request<any, Boolean>({
      url: `${BASE_URL}/audit`,
      method: "post",
      data,
    });
  },

  /**
 * 获取历史填报任务
 * @param taskId 任务Id
 */
  loadTaskRecordByTaskId(taskId: string) {
    return request<any, ActivityNodeTaskRecordDetail[]>({
      url: `${BASE_URL}/loadTaskRecordByTaskId?taskId=${taskId}`,
      method: "get",
    });
  },

    /**
 * 获取历史审批
 * @param taskId 任务Id
 */
  loadTaskAuditByTaskId(taskId: string) {
    return request<any, ActivityNodeTaskAudit[]>({
      url: `${BASE_URL}/loadTaskAuditByTaskId?taskId=${taskId}`,
      method: "get",
    });
  },
};

export default ActivityNodeTaskAPI;

/** 分页查询参数 */
export interface ActivityNodeTaskPageQuery extends PageQuery {
  /** 搜索关键字 */
  statuses?: string;
  relatedTypes?: string;
}


export interface AuditForm {
  taskId: string;
  remark: string;
  result: boolean;
}

/** 实例节点任务对象 */
export interface ActivityNodeTaskVO {
  /** ID */
  id: string;
  /** 实例ID */
  activityId: string;
  /** 节点ID */
  activityNodeId: string;
  /** 名称 */
  name: string;
  /** 描述 */
  remark: string;
  /** 执行人 */
  userId: string;
  username: string;
  /** 角色 */
  activityRole: string;
  /** 状态 */
  status: string;
  /** 候选执行人 */
  candidateId: string;
  candidateType: string;
  candidateName: string;
  candidateNum: number;
  /** 创建时间 */
  createTime: string;
  /** 节点开始时间 */
  startTime: string;
  /** 节点完成时间 */
  endTime: string;
  /** 审核人id */
  auditorId?: string;
  /** 审核人名称 */
  auditorName?: string;
  /** 审核类型 */
  auditType?: string;
  /** 是否有审核权限 */
  hasAuditRole: boolean;
  /** 填报权限 */
  hasFillRole: boolean;
  handlerUsername?: string;
}

export interface ActivityNodeTaskDetail extends ActivityNodeTaskVO {
  nodeName: string;
  activityName: string;
  activityRelatedId: string;
  activityRelatedType: string;
  dataConfigs: ActivityNodeTaskDataConfig[];
  records: ActivityNodeTaskRecordDetail[];
}

/** 任务表单 */
export interface ActivityNodeTaskForm {
  /** ID */
  id: string;
  /** 实例ID */
  activityId: string;
  /** 节点ID */
  activityNodeId: string;
  /** 名称 */
  name: string;
  /** 描述 */
  remark: string;
  /** 角色 */
  activityRole: string;
  /** 候选执行人 */
  candidateId: string;
  candidateType: string;
  candidateName: string;
  candidateNum: number;
  /** 审核人id */
  auditorId?: string;
  /** 审核人名称 */
  auditorName?: string;
  /** 审核类型 */
  auditType?: string;
  activityNodeTaskDataConfigs: ActivityNodeTaskDataConfig[];
}

/** 任务数据 填报规则 */
export interface ActivityNodeTaskDataConfig {
  /** ID */
  id?: string;
  /** 流程id */
  activityId?: string;
  /** 流程节点id */
  activityNodeId?: string;
  /** 任务id */
  activityTaskId?: string;
  /** 属性类型 */
  dataType: string;
  /** 属性 */
  property: string;
  /** 属性值 */
  val: string;
  /** 属性值数据，前端用 */
  valList?: any[];
  /** 备选项 */
  options: string;
  /** 备选项数据数组，前端用 */
  optionsList: string[];
}

/** 任务记录 */
export interface ActivityNodeTaskRecord {
  /** ID */
  id?: string;
  /** 任务id */
  taskId?: string;
  taskName?: string;
  /** 名称 */
  name: string;
  /** 描述 */
  remark: string;
  /** 执行人 */
  userId: string;
  username: string;
  /** 创建时间 */
  createTime: string;
}

export interface ActivityNodeTaskRecordDetail extends ActivityNodeTaskRecord {
  items: ActivityNodeTaskRecordItem[];
}

/** 任务记录项 */
export interface ActivityNodeTaskRecordItem {
  /** ID */
  id?: string;
  /** 任务id */
  taskId: string;
  taskName: string;
  /** 记录id */
  recordId: string;
  /** 属性类型 */
  dataType: string;
  /** 属性 */
  property: string;
  /** 属性值 */
  val: string;
}

/** 任务填报表单 */
export interface ActivityNodeTaskFillForm {
  /** ID */
  taskId: string;
  /** 备注 */
  remark: string;
  /** 填表项 */
  items: ActivityNodeTaskFillItemForm[];
}

/** 任务填报表单单项 */
export interface ActivityNodeTaskFillItemForm {
  /** 属性类型 */
  dataType: string;
  /** 属性 */
  property: string;
  /** 属性值 */
  val: string;
}

export interface ActivityNodeTaskAudit {
  /** 任务Id */
  taskId?: string;
  /** 任务名称 */
  taskName?: string;
  auditorId?: string;
  auditorName?: string;
  result?: boolean;
  remark?: string;
  createTime?: string;

}

export enum ActivityNodeTaskStatus {
  WAIT_PRE = "WAIT_PRE",
  NOT_START = "NOT_START",
  PROCESSING = "PROCESSING",
  AUDITING = "AUDITING",
  COMPLETED = "COMPLETED",
}

export const ActivityNodeTask_Status_Label: IObject = {
  [ActivityNodeTaskStatus.WAIT_PRE]: "等待中",
  [ActivityNodeTaskStatus.NOT_START]: "未开始",
  [ActivityNodeTaskStatus.PROCESSING]: "进行中",
  [ActivityNodeTaskStatus.AUDITING]: "审批中",
  [ActivityNodeTaskStatus.COMPLETED]: "已完成",
};
